﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Jarvis.Server.Workers.Messages;
using Ninject.Extensions.Logging;

namespace Jarvis.Server.Workers
{
    public class ActionsWorker : Worker
    {
        private readonly ILogger _logger;

        public ActionsWorker(ILogger logger )
        {
            _logger = logger;
            Subscribe<CurrentLocationChangedMessage>(this);
            Name = "Actions Worker";
        }

        #region Overrides of Worker
        public override void Loop()
        {
            Thread.Yield();
        }

        public override void OnMessageReceived(Messages.Message message)
        {
            base.OnMessageReceived(message);

            var currentLocationChangedMessage = message as CurrentLocationChangedMessage;
            if(currentLocationChangedMessage!=null)
            {
                foreach (var action in currentLocationChangedMessage.Location.Actions)
                {
                    _logger.Trace("Executing action {0}", action.Name);
                    action.Execute();
                }
            }
        }

        #endregion
    }
}
